package org.jeecg.modules.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.gas.VO.gasRatioVO;
import org.jeecg.modules.api.entity.CyGasStore;
import org.jeecg.modules.gas.service.ICyGasStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 温室气体库
 * @Author: jeecg-boot
 * @Date:   2024-11-06
 * @Version: V1.0
 */
@Api(tags="温室气体库")
@RestController
@RequestMapping("/gas/cyGasStore")
@Slf4j
public class CyGasStoreController extends JeecgController<CyGasStore, ICyGasStoreService> {
	@Autowired
	private ICyGasStoreService cyGasStoreService;
	
	/**
	 * 分页列表查询
	 *
	 * @param cyGasStore  温室气体对象
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "温室气体库-分页列表查询")
	@ApiOperation(value="温室气体库-分页列表查询", notes="温室气体库-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CyGasStore>> queryPageList(CyGasStore cyGasStore,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		QueryWrapper queryWrapper = new QueryWrapper();
		if(cyGasStore.getGasName()!=null){         //如果传了温室气体名称，就创建模糊查询条件
			queryWrapper.like("gas_name", cyGasStore.getGasName());
		}
		queryWrapper.orderByDesc("create_time");
		Page<CyGasStore> page = new Page<CyGasStore>(pageNo, pageSize);
		IPage<CyGasStore> pageList = cyGasStoreService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param cyGasStore 温室气体对象
	 * @return
	 */
	@AutoLog(value = "温室气体库-添加")
	@ApiOperation(value="温室气体库-添加", notes="温室气体库-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:cy_gas_store:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CyGasStore cyGasStore) {
		cyGasStoreService.add(cyGasStore);
		return Result.OK("添加成功！");
	}

	 /**
	  *   新增碳排因子，数据详情中气体配比中选择气体的接口
	  * @param factorRealUnit  碳排因子实际单位值（用来查询出相同单位的温室气体）
	  * @return
	  */
	 @ApiOperation(value="温室气体库-选择碳排因子气体配比", notes="温室气体库-选择碳排因子气体配比")
	 @GetMapping(value = "/listGasName")
	 public Result<?> listGasName(@RequestParam(name="factorRealUnit",required=true) String factorRealUnit) {
		 List<gasRatioVO> gasRatioVOS = cyGasStoreService.listGasName(factorRealUnit);
		 return Result.OK(gasRatioVOS);
	 }


	/**
	 *  编辑
	 *
	 * @param cyGasStore 温室气体对象
	 * @return
	 */
	@AutoLog(value = "温室气体库-编辑")
	@ApiOperation(value="温室气体库-编辑", notes="温室气体库-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:cy_gas_store:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CyGasStore cyGasStore) {
		cyGasStoreService.updateById(cyGasStore);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id 温室气体id
	 * @return
	 */
	@AutoLog(value = "温室气体库-通过id删除")
	@ApiOperation(value="温室气体库-通过id删除", notes="温室气体库-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:cy_gas_store:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		cyGasStoreService.delete(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "温室气体库-批量删除")
	@ApiOperation(value="温室气体库-批量删除", notes="温室气体库-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:cy_gas_store:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cyGasStoreService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "温室气体库-通过id查询")
	@ApiOperation(value="温室气体库-通过id查询", notes="温室气体库-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CyGasStore> queryById(@RequestParam(name="id",required=true) String id) {
		CyGasStore cyGasStore = cyGasStoreService.getById(id);
		if(cyGasStore==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(cyGasStore);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cyGasStore
    */
    //@RequiresPermissions("org.jeecg.modules.demo:cy_gas_store:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CyGasStore cyGasStore) {
        return super.exportXls(request, cyGasStore, CyGasStore.class, "温室气体库");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("cy_gas_store:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CyGasStore.class);
    }

}
